package com.kolakcc.loljclient.model;

import java.util.Date;
import java.util.Vector;

import com.gvaneyck.rtmp.TypedObject;

//TODO: recheck RecentGame model for more fields
public class RecentGame extends Game implements Comparable<RecentGame> {
	public TypedObject internalTypedObject;

	public String gameType;
	public int experienceEarned;
	public boolean eligibleFWOTD;
	public Object difficulty;
	public boolean leaver;
	public int spell1;
	public int teamID;
	public Vector<Statistic> statistics = new Vector<Statistic>();
	public int spell2;
	public boolean afk;
	public int dataVersion;
	public Date createDate;
	public int userServerPing;
	public int adjustedRating;
	public int premadeSize;
	public int boostXPEarned;
	public int boostIPEarned;
	public int gameID;
	public int timeInQueue;
	public int IPearned;
	public int eloChange; // probably doesn't work anymore
	public Object futureData;
	public String gameMode;
	public String difficultyString;
	public int teamRating;
	public String subType;
	public String queueType;
	public boolean premadeTeam;
	public int rating;
	public int championID;
	public boolean invalid;
	public int level;

	// ???
	public Object rawStatsJSON;
	public int summonerID; // 0?
	public String gameTypeEnum; // same as up there?
	public int ID; // 0?
	public double KCoefficient;
	public double predictedWinPct;
	public int userID;

	public RecentGame(TypedObject to) {
		this.internalTypedObject = to;

		this.gameType = to.getString("gameType");
		this.experienceEarned = to.getInt("experienceEarned");
		this.rawStatsJSON = to.get("rawStatsJson");
		this.eligibleFWOTD = to.getBool("eligibleFirstWinOfDay");
		this.difficulty = to.getString("difficulty");
		this.gameMapID = to.getInt("gameMapId");
		this.leaver = to.getBool("leaver");
		this.spell1 = to.getInt("spell1");
		this.gameTypeEnum = to.getString("gameTypeEnum");
		this.teamID = to.getInt("teamId");
		this.summonerID = to.getInt("summonerId");
		this.afk = to.getBool("afk");
		this.boostXPEarned = to.getInt("boostXpEarned");
		this.level = to.getInt("level");
		this.invalid = to.getBool("invalid");
		this.dataVersion = to.getInt("dataVersion");
		this.userID = to.getInt("userId");
		this.createDate = to.getDate("createDate");
		this.userServerPing = to.getInt("userServerPing");
		this.adjustedRating = to.getInt("adjustedRating");
		this.premadeSize = to.getInt("premadeSize");
		this.boostIPEarned = to.getInt("boostIpEarned");
		this.gameID = to.getInt("gameId");
		this.timeInQueue = to.getInt("timeInQueue");
		this.IPearned = to.getInt("ipEarned");
		this.eloChange = to.getInt("eloChange");
		this.futureData = to.getString("futureData");
		this.gameMode = to.getString("gameMode");
		this.difficultyString = to.getString("difficultyString");
		this.KCoefficient = to.getDouble("KCoefficient");
		this.teamRating = to.getInt("teamRating");
		this.subType = to.getString("subType");
		this.queueType = to.getString("queueType");
		this.premadeTeam = to.getBool("premadeTeam");
		this.predictedWinPct = to.getDouble("predictedWinPct");
		this.rating = to.getInt("rating");
		this.championID = to.getInt("championId");
		Object[] stats = to.getArray("statistics");
		for (Object stat : stats) {
			TypedObject castedStat = (TypedObject) stat;
			this.statistics.add(new Statistic(castedStat));
		}
		System.out.println(to);
	}

	@Override
	public int compareTo(RecentGame o) {
		RecentGame otherGame = o;
		return this.createDate.compareTo(otherGame.createDate);
	}

	public int getStatistic(String key) {
		for (Statistic s : this.statistics) {
			if (s.statName.equals(key)) {
				return s.value;
			}
		}
		return 0;
	}
	
	public String toString() {
		return String.format("%s - %d/%d/%d", Champion.getChampionFromID(this.championID).getDisplayName(),this.getStatistic("CHAMPIONS_KILLED"),this.getStatistic("NUM_DEATHS"),this.getStatistic("NUM_ASSISTS"));
	}
}
